<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\erp\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
/**
 * 单品详情
 * @package app\erp\admin
 */
class GoodsSku extends Admin
{
	
	/**
     * 单品列表
     * @author shiqiang
     * @return mixed
     */
    public function index(){	
		cookie('__forward__', $_SERVER['REQUEST_URI']);
		
		// 获取筛选
        $map = $this->getMap();
		
		//数据获取
        $data = input();
		
		$goods_id = isset($data['goods_id']) ? $data['goods_id'] : 0;
		$map[] = ['goods_id', '=', $goods_id];
		
		// 数据列表
		$data_list = model('erp/ErpGoodsSku', 'model') -> where($map) -> order('create_time desc') -> paginate();
		foreach($data_list as $key => $value){
			// $data_list[$key]['goods_specifications'] 	= $value['goods_specifications'] == '' ? $value['goods_name'] : $value['goods_specifications'];
		}
		
		
		$btn_access = [
			'title' => '编辑',
			'icon'  => 'fa fa-fw fa-key',
			'class' => 'btn btn-xs btn-default',
			'href'  => url('edit', ['id' => '__id__']),
		];
		// 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('商品列表') // 设置页面标题
            ->setTableName('erp_goods_sku') // 设置数据表名
            ->addColumns([ // 批量添加列
				['id', '规格id'],
				['goods_name', '商品名称'],
                ['goods_specifications', '规格名称','text.edit'],
                // ['code', '单品编码'],
                // ['cover_url', '图片', 'picture'],
				['article_no', '国际条形码','text.edit'],
				//['weight', '重量','text.edit'],
				//['volume', '体积','text.edit'],
				['purchase_price', '进价','text.edit'],
				['price', '售价','text.edit'],
				['sku_status', '是否可售','select', [ 1 => '可售', 2 => '不可售']],
				['right_button', '操作', 'btn']
            ])
			->addRightButton('custom', $btn_access, true) // 添加授权按钮
			->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染页面
	}
	
	public function edit($id = ''){
		if ($id === '') $this->error('缺少参数');
		// 保存数据
		if ($this->request->isPost()) {
			$data = $this->request->post('', null, 'trim');
			$data['id'] = $id;
			$goods_specifications = isset($data['goods_specifications']) ? $data['goods_specifications'] : '';
			$article_no  		= isset($data['article_no']) ? $data['article_no'] : '';
			$purchase_price  	= isset($data['purchase_price']) ? $data['purchase_price'] : '';
			$price  			= isset($data['price']) ? $data['price'] : '';
			$sku_status  		= isset($data['sku_status']) ? $data['sku_status'] : '';
			
			if($goods_specifications == ''){
				$this->error('请输入规格名称');
			}
			if($purchase_price == ''){
				$this->error('请输入进价');
			}
			if($price == ''){
				$this->error('请输入售价');
			}
			$map = [];
			$map['id'] = $id;
			
			$data = [];
			$data['goods_specifications'] 	= $goods_specifications;
			$data['article_no'] 			= $article_no;
			$data['purchase_price'] 		= $purchase_price;
			$data['price'] 					= $price;
			$data['sku_status'] 			= $sku_status;
			
			$rel = model('ErpGoodsSku') -> update($data, $map);
			if($rel){
				
				$this->success('编辑成功', '', '_parent_reload');
			}else{
				$this->error($rel['message']);
			}
		}
		
		$info = model('ErpGoodsSku') -> find($id);
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑')
            ->addFormItems([
				['text', 'goods_specifications', '规格名称'],
				['text', 'article_no', '条形码'],
				['text', 'purchase_price', '进价'],
				['text', 'price', '售价'],
				['select', 'sku_status', '是否可售', '', [ 1 => '可售', 2 => '不可售']],
            ])
			// ->addText('sort', '排序')
			->setFormData($info)
			->fetch();
	}
          
}

